/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       mini_map_window.cpp

	$Header: /game/mini_map_window.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "mini_map_window.h"

#include "adventure_frame.h"
#include "adventure_map.h"
#include "adventure_map_window.h"
#include "convert_16_bit.h"
#include "mouse_event.h"
#include "paint_surface.h"
#include "platform.h"
// --------------------------------------------------------------------
// window to display a minimap
// --------------------------------------------------------------------
t_mini_map_window::t_mini_map_window( t_adventure_map const& map, t_screen_rect const& rect, 
		                              t_adventure_map_window* map_window, t_window* parent )
				 : t_window( k_no_transparency, parent), t_mini_map_renderer( map, map_window->get_frame()->get_team_view() ),
				   m_map_window( map_window )
{
	init( rect );
	set_viewport_rect( t_screen_rect( map_window->get_view_pos(), map_window->get_view_size() ) );
	resize( rect.size() );
	m_dragging = false;
}
// --------------------------------------------------------------------
// window to display a minimap
// --------------------------------------------------------------------
t_mini_map_window::t_mini_map_window( t_adventure_map const& map, t_screen_rect const& rect, 
		                              t_adventure_map_window* map_window, t_window* parent, int team_view )
				 : t_window( k_no_transparency, parent), t_mini_map_renderer( map, team_view ),
				   m_map_window( map_window )
{
	init( rect );
	set_viewport_rect( t_screen_rect( map_window->get_view_pos(), map_window->get_view_size() ) );
	resize( rect.size() );
	m_dragging = false;
}

// --------------------------------------------------------------------
// window to display a minimap
// --------------------------------------------------------------------
void t_mini_map_window::paint( t_paint_surface& paint_surface )
{
	update( paint_surface.get_rect() );
	paint_surface.draw( t_screen_point(0,0), *m_back_buffer, m_back_buffer->get_rect() );
}

	// Member functions
// --------------------------------------------------------------------
// window to display a minimap
// --------------------------------------------------------------------
t_shared_ptr< t_abstract_bitmap16 > t_mini_map_window::create_back_buffer(
													   t_screen_point const & size )
{
	m_back_buffer = new t_memory_bitmap16( size.x, size.y );
	return m_back_buffer;
}

// --------------------------------------------------------------------
// window to display a minimap
// --------------------------------------------------------------------
void t_mini_map_window::on_rect_dirtied( t_screen_rect const & rect )
{
	invalidate( rect );
}

// --------------------------------------------------------------------
// window to display a minimap
// --------------------------------------------------------------------
void t_mini_map_window::on_size_change( t_screen_rect const& rect )
{
	resize( rect.size() );
}

// --------------------------------------------------------------------
// window to display a minimap
// --------------------------------------------------------------------
void t_mini_map_window::on_view_level_changed( int level )
{
}

// --------------------------------------------------------------------
// window to display a minimap
// --------------------------------------------------------------------
void t_mini_map_window::on_view_resized( t_screen_point const & size )
{
}
// --------------------------------------------------------------------
// window to display a minimap
// --------------------------------------------------------------------
void t_mini_map_window::center_view( t_screen_point point )
{
	if (!m_dragging)
		return;
	
	t_screen_point map_size = get_adventure_map().get_view_size();
	t_screen_point mini_map_size = get_view_size();

	point.x = point.x * map_size.x / mini_map_size.x;
	point.y = point.y * map_size.y / mini_map_size.y;

	m_map_window->center_view( point );
	set_viewport_rect( t_screen_rect( m_map_window->get_view_pos(), m_map_window->get_view_size() ) );
}


void t_mini_map_window::resize_view(t_screen_rect const& new_size_pos)
{
	set_viewport_rect( new_size_pos );
}

// --------------------------------------------------------------------
// window to display a minimap
// --------------------------------------------------------------------
void t_mini_map_window::left_button_down( t_mouse_event const& event )
{
	m_dragging = true;
	center_view( event.client_point );
}

// --------------------------------------------------------------------
// window to display a minimap
// --------------------------------------------------------------------
void t_mini_map_window::left_button_up( t_mouse_event const& event )
{
	m_dragging = false;
}

// --------------------------------------------------------------------
// window to display a minimap
// --------------------------------------------------------------------
void t_mini_map_window::mouse_move( t_mouse_event const& event )
{
	if (!event.left_button)
	{
		m_dragging = false;
		return;
	}
	if (event.client_point != get_mouse_position( this ))
		return;
	center_view( event.client_point );
}
